<template>  
        <div class="login">
        </div>
</template>

<script>
import Input from "@com/Input/index.vue"
import {useRouter} from "vue-router"
import { reactive, toRefs,inject} from 'vue'
import { Toast } from 'vant';
import {authorizations, me} from "../../services/api"
export default {
    setup(){
        const router = useRouter()
        let fromdata = reactive({
            name:'',
            password:''
        })
        let loading = inject('loading', {})
        
        const rules = () => {
            if (fromdata.name == "") {
                Toast("Account number Cannot be empty!");
                return false;
            }
            if (fromdata.password == "") {
                Toast("Password Cannot be empty!");
                return false;
            }
            return true;
        }

        const to_sign = (url)=>router.push({
            path:url
        })

        const log = () => {
            if(rules()){
                    loading.value = true
                    authorizations(fromdata).then(res => {
                        if(res.code==0){
                            localStorage.setItem("token",res.data.access_token)
                            me().then(res1 =>{
                                if(res1.code==0){
                                    localStorage.setItem("person",JSON.stringify(res1.data))
                                    sessionStorage.removeItem("hash")
                                    loading.value = false
                                    Toast("Log In Successful!")
                                    router.push("/")
                                }
                                else{
                                Toast(res1.msg)
                                loading.value = false
                                }
                            },err => {
                                Toast(err.msg)
                                loading.value = false
                            })
                            
                        }
                        else{
                        Toast(res.msg)
                        loading.value = false
                        }
                    }, err =>{
                        Toast(err.msg)
                        loading.value = false
                    })
                }            
        }

        return {
            ...toRefs(fromdata),
            to_sign,
            log
        }
    },
    components:{
        Input,
    }
}
</script>

<style lang="scss" scoped>
    .login{
        width: 100%;
        height: 100vh;
        padding:32px;
        box-sizing: border-box;
        overflow: hidden;
        background: rgb(255, 255, 255);
        font-size: 36px;
        font-weight: bold;

    }
</style>